//
//  SubmitViewController.m
//  wtshoppingcite
//
//  Created by TW on 14-12-2.
//  Copyright (c) 2014年 TW. All rights reserved.
//

#import "SubmitViewController.h"
#import "ShoppingCartModel.h"
#import "RegisterHelper.h"
#import "DeliveryViewController.h"
#import "AdispatchView.h"
#import "AddressModel.h"
#import "PaymentViewController.h"
#import "UIImage+category.h"
@interface SubmitViewController ()<UITableViewDataSource,UITableViewDelegate,deliveryViewDelegate>
{
    UITableView *submitTableView;
    UIView *footView;
    UILabel *addr;
    UIButton *addAddressButton;
}

@end

@implementation SubmitViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setTitleView:@"提交订单"];
    self.view.backgroundColor = [UIColor whiteColor];
    
    submitTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight-60) style:UITableViewStylePlain];
    submitTableView.dataSource = self;
    submitTableView.delegate = self;
    submitTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:submitTableView];
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, kHeight-60, kWidth, 60)];
    view.backgroundColor = [UIColor clearColor];
    [self.view addSubview:view];
    
    UIView *seperateView = [[UIView alloc]initWithFrame:CGRectMake(0, 2, kWidth, 1)];
    seperateView.backgroundColor = UIColorFromRGB(0x95a0a0);
    [view addSubview:seperateView];
    
    UIButton *totalButton = [[UIButton alloc]initWithFrame:CGRectMake(5, 10, 80, 40)];
    totalButton.right = kWidth -10;
    [totalButton setTitle:@"提交订单" forState:UIControlStateNormal];
    [totalButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [totalButton setBackgroundImage:[UIImage imageNamed:@"register_select"] forState:UIControlStateNormal];
    [totalButton addTarget:self action:@selector(commitAction) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:totalButton];
    
    UILabel *totalLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, (60-20)/2, 40, 20)];
    totalLabel.right = totalButton.left - 170;
    totalLabel.text = @"合计:";
    totalLabel.textColor = MAIN_BLUE_COLOR;
    [view addSubview:totalLabel];
    
    UIImageView *iconImage = [[UIImageView alloc]initWithFrame:CGRectMake(totalLabel.right, totalLabel.top+5, 10, 10)];
    iconImage.image = [UIImage imageNamed:@"money_code"];
    [view addSubview:iconImage];
    
    UILabel *oPirceLabel = [[UILabel alloc]initWithFrame:CGRectMake(iconImage.right,totalLabel.top, 100, 40)];
    oPirceLabel.bottom = iconImage.bottom + 15;
    oPirceLabel.text = [NSString stringWithFormat:@"%.2f",self.totalSum];
    oPirceLabel.textColor = MAIN_BLUE_COLOR;
    [view addSubview:oPirceLabel];
    oPirceLabel.font = [UIFont systemFontOfSize:21.0f];
    oPirceLabel.backgroundColor = [UIColor clearColor];
    
    
        // Do any additional setup after loading the view.
}



-(void)commitAction{
    if (addr.text !=nil) {
        
        PaymentViewController *paymentVC =[[PaymentViewController alloc]init];
        paymentVC.hidesBottomBarWhenPushed = YES;
        paymentVC.productsArray = self.dataArray;
        paymentVC.isPurchase = YES;
        paymentVC.type = NSShoppingPayForStyle;
        paymentVC.totalPrice = [NSString stringWithFormat:@"%.2f",self.totalSum];
        [self.navigationController pushViewController:paymentVC animated:YES];

    }else{
          [self showToastMessage:@"请选择收货地址"];
    }
}


#pragma mark tableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return self.dataArray.count;

    }else if(section == 1){
        return 2;
    }
    return 0;
}

#pragma mark tableViewDelgate
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdentitfer"];

    if (indexPath.section == 0) {
        
     if (cell == nil) {
            
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellIdentitfer"];
            
            UILabel *titeLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 200, 60)];
            titeLabel.numberOfLines = 0;
            titeLabel.tag = 100;
            titeLabel.font = [UIFont systemFontOfSize:18.0f];
            titeLabel.backgroundColor = [UIColor clearColor];
            titeLabel.textColor = UIColorFromRGB(0x9fa0a0);
            [cell.contentView addSubview:titeLabel];
            
            
            UIImageView *iconImage = [[UIImageView alloc]initWithFrame:CGRectMake(titeLabel.right, 20, 12, 12)];
            iconImage.right = kWidth - 50;
            iconImage.image = [UIImage imageNamed:@""];
            iconImage.tag = 1001;
            [cell.contentView addSubview:iconImage];
            
            UILabel *sPriceLable = [[UILabel alloc]initWithFrame:CGRectMake(iconImage.right, iconImage.top-20, 45, 45)];
            sPriceLable.numberOfLines = 0;
            sPriceLable.font = [UIFont systemFontOfSize:16.0f];
            sPriceLable.backgroundColor = [UIColor clearColor];
            sPriceLable.textColor = UIColorFromRGB(0x9fa0a0);
            sPriceLable.tag = 1002;
            [cell.contentView addSubview:sPriceLable];
            
            
            UILabel *totalLable = [[UILabel alloc]initWithFrame:CGRectMake(sPriceLable.left, sPriceLable.bottom - 20, 45, 45) ];
            totalLable.tag = 1003;
            totalLable.numberOfLines = 0;
            totalLable.font = [UIFont systemFontOfSize:12.0f];
            totalLable.backgroundColor = [UIColor clearColor];
            totalLable.textColor = UIColorFromRGB(0x9fa0a0);
            [cell.contentView addSubview:totalLable];
         
         UIView *separateView = [[UIView alloc]init];
         separateView.frame = CGRectMake(0,80 - 2, kWidth, 2);
         separateView.backgroundColor = UIColorFromRGB(0x9fa0a0);
         separateView.alpha =0.2;
         [cell.contentView addSubview:separateView];
        }
        
        ShoppingCartModel *shoppingCartModel = self.dataArray[indexPath.row];
        
        UILabel *textLabel = (UILabel *)[cell.contentView viewWithTag:100];
        textLabel.text =shoppingCartModel.title;
        
        UIImageView *iconImage = (UIImageView *)[cell.contentView viewWithTag:1001];
        iconImage.image = [UIImage imageNamed:@"money_code"];
        
        UILabel *sPriceLable = (UILabel *)[cell.contentView viewWithTag:1002];
        sPriceLable.text = shoppingCartModel.oPrice;
        
        UILabel *totalLabel = (UILabel *)[cell.contentView viewWithTag:1003];
        totalLabel.text =[NSString stringWithFormat:@"x%@",[[RegisterHelper shareHelper].shopCarNumbers objectForKey:shoppingCartModel.sid]];
        
        return cell;
        
    }else if(indexPath.section == 1){
     
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdentitfer1"];
        if (cell == nil) {
            
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellIdentitfer1"];
            
        }
        if (indexPath.row == 0) {
            
            UIView *separateView = [[UIView alloc]init];
            separateView.frame = CGRectMake(0,0, kWidth, 10);
            separateView.backgroundColor = UIColorFromRGB(0x9fa0a0);
            separateView.alpha =0.2;
            [cell.contentView addSubview:separateView];
            
            UILabel *addressLabel = [[UILabel alloc]initWithFrame:CGRectMake(6, 10,80, 40)];
            addressLabel.text = @"收货地址:";
            [cell.contentView addSubview:addressLabel];
            addressLabel.font = [UIFont systemFontOfSize:15.0f];
            addressLabel.backgroundColor = [UIColor clearColor];
            addressLabel.textColor = UIColorFromRGB(0x9fa0a0);
            
            
            addr = [[UILabel alloc]initWithFrame:CGRectMake(addressLabel.right, 6,200, 60)];
            addr.numberOfLines = 0;
            [cell.contentView addSubview:addr];
            addr.font = [UIFont systemFontOfSize:12.0f];
            addr.backgroundColor = [UIColor clearColor];
            addr.textColor = UIColorFromRGB(0x9fa0a0);
            
    
            addAddressButton = [[UIButton alloc]initWithFrame:CGRectMake(6, 25, 160, 40)];
            addAddressButton.left = kWidth - 150;
            addAddressButton.tag = 10000;
            [addAddressButton setTitle:@"添加收货地址" forState:UIControlStateNormal];
            [addAddressButton setImage:[UIImage imageNamed:@"register_add"] forState:UIControlStateNormal];
            [addAddressButton setFont:[UIFont systemFontOfSize:15]];
            [cell.contentView addSubview:addAddressButton];
            [addAddressButton addTarget:self action:@selector(commonButtonAction:) forControlEvents:UIControlEventTouchUpInside];
            [addAddressButton setTitleColor: UIColorFromRGB(0x9fa0a0) forState:UIControlStateNormal];
            
            
            UIView *separateView2 = [[UIView alloc]init];
            separateView2.frame = CGRectMake(0,60 - 2, kWidth, 2);
            separateView2.backgroundColor = UIColorFromRGB(0x9fa0a0);
            separateView2.alpha =0.2;
            [cell.contentView addSubview:separateView2];

        }
        else if (indexPath.row == 1){
            UILabel *timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 6,100, 40)];
            timeLabel.text = @"配送时间:";
            [cell.contentView addSubview:timeLabel];
            timeLabel.font = [UIFont systemFontOfSize:15.0f];
            timeLabel.backgroundColor = [UIColor clearColor];
            timeLabel.textColor = UIColorFromRGB(0x9fa0a0);
            
            
            UIButton *adispatchButton = [[UIButton alloc]initWithFrame:CGRectMake(5, 6, 180, 40)];
            adispatchButton.left = timeLabel.right;
            adispatchButton.tag = 10001;
            [adispatchButton setTitle:@"工作日、节假日均可送货" forState:UIControlStateNormal];
            [adispatchButton setFont:[UIFont systemFontOfSize:15]];
            [cell.contentView addSubview:adispatchButton];
            [adispatchButton addTarget:self action:@selector(commonButtonAction:) forControlEvents:UIControlEventTouchUpInside];
            [adispatchButton setTitleColor: UIColorFromRGB(0x9fa0a0) forState:UIControlStateNormal];
            
            UIView *separateView = [[UIView alloc]init];
            separateView.frame = CGRectMake(0,60 - 10, kWidth, 10);
            separateView.backgroundColor = UIColorFromRGB(0x9fa0a0);
            separateView.alpha =0.2;
            [cell.contentView addSubview:separateView];
            

        }
        
        return cell;
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
         return 80;
    }else if(indexPath.section== 1){
        return 60;
    }
    return 0;
}


//-(UILabel*)createLabelWithFrame:(CGRect)frame text:(NSString*)text font:(int)font textAli:(NSTextAlignment)alig textColor:(UIColor*)tc{
//    if(text == nil)
//        text = @"";
//    UILabel* label = [[UILabel alloc] initWithFrame:frame];
//    label.text = text;
//    label.font = [UIFont systemFontOfSize:font];
//    label.textAlignment = alig;
//    label.textColor = tc;
//    label.numberOfLines = 0;
//    label.backgroundColor = [UIColor clearColor];
//    return label;
//}

//#pragma mark buttonAction
-(void)commonButtonAction:(UIButton *)sender{
    
    if (sender.tag == 10000) {
        
        DeliveryViewController *deliveryVC = [[DeliveryViewController alloc]init];
        deliveryVC.hidesBottomBarWhenPushed = YES;
        deliveryVC.delegate = self;
        [self.navigationController pushViewController:deliveryVC animated:YES];
        
    }else if (sender.tag == 10001){
        
        AdispatchView *adispatchView = [[AdispatchView alloc]initWithFrame:CGRectMake(20, kHeight/2 - 44*5/2, kWidth - 40, 44*5)];
        [adispatchView showView:self.view];
        adispatchView.titleArray = @[@"立即配送",@"电话预约",@"只双休日、节假日送货",@"工作日、节假日均可送货",@"只工作日送货"];
        adispatchView.button = sender;
        [self.view addSubview:adispatchView];
        
    }
}

#pragma mark deliveryDelegate
-(void)commitAddress:(NSString *)address{
    
    addr.text = address;
    [addAddressButton setTitle:@""  forState:UIControlStateNormal];
    [addAddressButton setImage:[UIImage imageNamed:@"white"] forState:UIControlStateNormal];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}


- (void)viewWillDisappear:(BOOL)animated
{
    
    [super viewWillDisappear:animated];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
